package com.ytjj.qmyx.users.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @version 1.0
 *
 */
public class DateUtils {

    /**
     * .
     * @param oldTime 较小的时间
     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     */
    public static int isYeaterday(Date oldTime, Date newTime) throws ParseException {
        if(newTime==null){
            newTime=new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = format.parse(todayStr);
        //昨天 86400000=24*60*60*1000 一天
        if((today.getTime()-oldTime.getTime())>0 && (today.getTime()-oldTime.getTime())<=86400000) {
            return 0;
        }
        else if((today.getTime()-oldTime.getTime())<=0){ //至少是今天
            return -1;
        }
        else{ //至少是前天
            return 1;
        }

    }

    /**
     * 今天开始时间到当前时间
     * @return
     */
    public static Map<String, Object> curDay() {
        LocalDate ld = LocalDate.now(ZoneId.systemDefault());
        LocalDateTime localDateTime = ld.atStartOfDay();

        Date startTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        LocalDateTime ldt_end = LocalDateTime.now(ZoneId.systemDefault());
        Date endTime = Date.from(ldt_end.atZone(ZoneId.systemDefault()).toInstant());

        Map<String, Object> result = new HashMap<>();
        result.put("dayStart", startTime);
        result.put("dayEnd", endTime);
        return result;
    }

    /**
     * 得到上周开始截至时间
     * @return
     */
    public static Map<String, Object> upWeek() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");//注意后面有一个空格
        Calendar calStart = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        calStart.add(Calendar.DAY_OF_MONTH,-7);//上一周
        calEnd.add(Calendar.DAY_OF_MONTH,-7);
        calStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calEnd.setFirstDayOfWeek(Calendar.MONDAY);
        calEnd.set(Calendar.DAY_OF_WEEK, calEnd.getFirstDayOfWeek() + 6);
        Timestamp weekStart = Timestamp.valueOf(format.format(calStart.getTime()) + "00:00:00");//字符串类型转化为timestamp
        Timestamp weekEnd = Timestamp.valueOf(format.format(calEnd.getTime()) + "23:59:59");
        System.out.println(weekStart);
        System.out.println(weekEnd);

        Map<String, Object> result = new HashMap<>();
        result.put("weekStart", weekStart);
        result.put("weekEnd", weekEnd);
        return result;
    }

    /**
     * 本周开始时间
     * @return
     */
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }
}
